/**
 * 
 */
package com.runningfox.humor;

import android.content.Context;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;

import com.runningfox.logic.Logic;
import com.runningfox.model.Setting;

/**
 * @author luhaiyang
 * 
 */
public class Settings extends PreferenceActivity {
	String PREFS_NAME = "setting";
//	EditTextPreference namePref;
	Preference deleteCache;
	Preference mailtoauthor;
	Context c ;
	CheckBoxPreference networkPrefrence,themePrefrence;
//	TextView txtName;
//	EditText editName;
//	ImageView imgEditName,imgUpdateName,imgClose;
//	ProcessPool pool ;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.c =this.getApplicationContext();
		Logic.c =this.c;
//		Cache.DIR = c.getDir("cache", 0);
//		Cache.HOME = c.getDir("home", 0);
		this.addPreferencesFromResource(R.xml.setting);
//		this.setContentView(R.layout.setting);
		Setting.LoadSetting(this);
//		this.namePref = (EditTextPreference) this.findPreference("username");
//		this.namePref.setSummary(Setting.name);
		
		this.deleteCache = this.findPreference("emptycache");
		this.deleteCache.setSummary(Logic.getCacheSize()/1000+"KB");
		this.deleteCache.setOnPreferenceClickListener(new OnPreferenceClickListener(){

			@Override
			public boolean onPreferenceClick(Preference preference) {
				// TODO Auto-generated method stub
				Logic.clearCache();
				deleteCache.setSummary(Logic.getCacheSize()/1000+"KB");
				return false;
			}});
		this.networkPrefrence = (CheckBoxPreference) this.findPreference("network");
		this.networkPrefrence.setChecked(Setting.autoConnect);
		this.networkPrefrence.setOnPreferenceClickListener(new OnPreferenceClickListener(){

			@Override
			public boolean onPreferenceClick(Preference preference) {
				// TODO Auto-generated method stub
				if(!networkPrefrence.isChecked()){
					networkPrefrence.setChecked(false);
					Setting.autoConnect=false;
					Setting.saveSetting(Settings.this);
				}else{
					networkPrefrence.setChecked(true);
					Setting.autoConnect=true;
					Setting.saveSetting(Settings.this);
				}

				return true;
			}
			
		});
		
		this.themePrefrence= (CheckBoxPreference) this.findPreference("theme");
		this.themePrefrence.setChecked(Setting.theme);
		this.themePrefrence.setOnPreferenceClickListener(new OnPreferenceClickListener(){

			@Override
			public boolean onPreferenceClick(Preference preference) {
				// TODO Auto-generated method stub
				if(!themePrefrence.isChecked()){
					themePrefrence.setChecked(false);
					Setting.theme=false;
					Setting.saveSetting(Settings.this);
				}else{
					themePrefrence.setChecked(true);
					Setting.theme=true;
					Setting.saveSetting(Settings.this);
				}
				return true;
			}
			
		});
	}
}
